package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;


import ModelLayer.Supplier;

public class DBSupplier {
	private static Connection con;
	Statement stmt;
	public DBSupplier() 
	{
		con = DbConnection.getInstance().getDBcon();
	}
	public void createSupplier(String name,String address,String country,String e_mail,int phone)
	{
		try{
			int suID=GetMax.getMaxId("Select max(suID) from Supplier")+1;//Gets the maximum suID from the DB and adds+1 for the next ID
			String query ="INSERT INTO Supplier(suID, name, address, country, e_mail, phone)  VALUES('"+suID+"','"+name+"','"+address+"','"+country+"','"+e_mail+"','"+phone+"')";
			 Statement stmt = con.createStatement();
	          stmt.setQueryTimeout(5);
	     	  stmt.executeUpdate(query);
	          stmt.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			}
	}
	public void deleteSupplier(int suID)
	{	//suID=1; 
		try{
			String query="DELETE FROM Supplier WHERE suID = '"+suID+"'";
			Statement stmt = con.createStatement();
	          stmt.setQueryTimeout(5);
	     	  stmt.executeUpdate(query);
	          stmt.close();
		}
		catch(Exception e)
		{e.printStackTrace();
		}
	}
	public void updateSupplier(Supplier g)
	{ Supplier suObj = g;
	String query="UPDATE Supplier SET "+
		 	  "name ='"+ suObj.getName()+"', "+
		 	  "address ='"+ suObj.getAddress() + "'" +
		 	 "country ='"+ suObj.getCountry() + "'" +
		 	"e_mail ='"+ suObj.getE_mail() + "'" +
		 	"phone ='"+ suObj.getPhone() + "'" +
		          " WHERE suID = '"+ suObj.getSuID() + "'";
              System.out.println("Update query:" + query);
              try{
            	  Statement stmt = con.createStatement();
            	  stmt.setQueryTimeout(5);
            	  stmt.executeUpdate(query);
            	  stmt.close();
            	  
              }
              catch(Exception e)
              {e.printStackTrace();}
		
	}
	
	private String buildQuery()
	{
	    String query="SELECT suID,name,address,country,e_mail,phone  FROM Supplier";
		/*
		if (wClause.length()>0)
			query=query+" WHERE ";
			*/
		return query;
	}
	public ArrayList<Supplier> miscWhere()
	{
            ResultSet results;
	    ArrayList<Supplier> list = new ArrayList<Supplier>();	
		
	    String query =  buildQuery();
  
            try{ // read the employee from the database
		Statement stmt = con.createStatement();
	 	stmt.setQueryTimeout(5);
	 	results = stmt.executeQuery(query);
	 	
	
		while( results.next() ){
	     	 Supplier empObj = new Supplier();
		 empObj = buildSupplier(results);	
                 list.add(empObj);	
		}//end while
                 stmt.close();     
                 
			
		}//slut try	
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	public Supplier findSupplier(int suID)
	{
		ResultSet results;
		Supplier empObj = new Supplier();
                
	        String query =  buildQuery() + " WHERE suID ="+"'"+suID+"'";
                System.out.println(query);
		try{ // 
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
                            empObj = buildSupplier(results);
                            //assocaition is to be build
                            stmt.close();
                            
                            
			}
                        else{ //no employee was found
                            empObj = null;
                        }
		}//end try	
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return empObj;
	}
	private Supplier buildSupplier(ResultSet results)
    {   Supplier suObj = new Supplier();
        try{ // the columns from the table glayee  are used
              suObj.setSuID(results.getInt("suID"));
              suObj.setName(results.getString("name"));
              suObj.setAddress(results.getString("address"));
              suObj.setCountry(results.getString("country"));
              suObj.setE_mail(results.getString("e_mail"));
              suObj.setPhone(results.getInt("phone"));
              
        }
       catch(Exception e)
       {
           System.out.println("error in building the Supplier object");
       }
       return suObj;
}
	
}